from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
# from info import app, db
from info import create_app, db, models
from info.models import User

# 这里导入models仅仅是为了迁移时，让manage知道models的存在

# 创建app
app = create_app('dev')

# 创建脚本管理对象
manager = Manager(app)
# 让迁移和app和数据库建立管理
Migrate(app, db)
# 让迁移和APP数据库建立迁移
manager.add_command('mysql', MigrateCommand)


# 脚本的使用：python manage.py createsuperuser -u admin -p 000000 -m 17611110000
@manager.option('-u', '-username', dest='username')
@manager.option('-p', '-password', dest='password')
@manager.option('-m', '-mobile', dest='mobile')
def createsuperuser(username, password, mobile):
    """创建超级管理员用户的脚本函数"""

    if not all([username, password, mobile]):
        print('缺少必传参数')
    else:
        user = User()
        user.nick_name = username
        user.password = password
        user.mobile = mobile
        # 当is_admin为Ture时，才是管理员用户
        user.is_admin = True

        try:
            db.session.add(user)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)


# 判断当前的__name__ 是否是__main__
if __name__ == '__main__':
    print(app.url_map)

    manager.run()
